package dyyx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

public class ConsumerTest {

	public static void main(String[] args) throws Exception {

		Properties props = new Properties();

		props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaUtil.SERVERS);
		props.put(ConsumerConfig.GROUP_ID_CONFIG, "kafkademo_test_group");
		props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
		// props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
		props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
		props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

		KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
		consumer.subscribe(Arrays.asList(KafkaUtil.TOPIC));
		
		System.out.println("consumer="+consumer);
		
		Collection<TopicPartition> partitions = new ArrayList<>();
		String topic = KafkaUtil.TOPIC;
		for(int i=0;i<6;i++){
			partitions.add(new TopicPartition(topic,i));
		}
		
		Map<TopicPartition, Long> beginningOffsets = consumer.beginningOffsets(partitions);
		Map<TopicPartition, Long> endOffsets = consumer.endOffsets(partitions);
		
		System.out.println("beginningOffsets="+beginningOffsets);
		System.out.println("endOffsets="+endOffsets);

		
		

		while (true) {
			ConsumerRecords<String, String> records = consumer.poll(100);
			for (ConsumerRecord<String, String> record : records) {
				System.out.println(record);
				// Thread.sleep(1000L);
			}
		}

	}

}
